package customerEnter.Frame.Utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

import customerEnter.Frame.DaoDataTransmission.WebAppsInfo;

public class DownloadImage {
	public static void main(String[] args) throws Exception {
		String str = download(
				"http://wx.qlogo.cn/mmopen/PiajxSqBRaEJyKwy9icOHpLvP9u7zr5HNuRkqdMawia1tslInsgK3EgbCtGjIHxJ695tPo9pKRYLdz8ibLeP5UZBGQ/0",
				"oCCUrws7h4Cog9domiGUEooUY7us");
		System.out.println(str);
	}

	public static String download(String urlString, String openid) throws Exception {
		String filename = openid + ".jpg";
		String savePath = WebAppsInfo.webUrl + "DownloadImage";
		// 构造URL
		URL url = new URL(urlString);
		// 打开连接
		URLConnection con = url.openConnection();
		// 设置请求超时为5s
		con.setConnectTimeout(5 * 1000);
		// 输入流
		InputStream is = con.getInputStream();

		// 1K的数据缓冲
		byte[] bs = new byte[1024];
		// 读取到的数据长度
		int len;
		// 输出的文件流
		File file = new File(savePath);
		if (!file.exists()) {
			file.mkdirs();
		}
		OutputStream os = new FileOutputStream(file.getPath() + "\\" + filename);
		// 开始读取
		while ((len = is.read(bs)) != -1) {
			os.write(bs, 0, len);
		}
		// 关闭
		os.close();
		is.close();
		return filename;
	}
}